package housing.task5.jaxb;

import housing.task1.actor.Renter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Class for serializing a collection<Obj> to xml file
 * <p/>
 * <p/>
 * <p/>
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 28.11.2010
 * Time: 14:53:20
 * <p/>
 */
public class Serializer {
    /**
     * If outputFile doesn't exist --> create it and marshall collection<Obj> into it
     * Else -->
     * unmarshall file content into temp collection,
     * add new records into temp collection
     * rewrite temp collection into output file
     *
     * @param renters
     * @param outFilePath
     */
    public static void serialize(Renters renters, String outFilePath) {
        File file = new File(outFilePath);
//        FileWriter out = null;

        try {
//            out = new FileWriter(file, true);

            if (!file.exists()) {
                if (file.createNewFile()) {
                    System.out.println("File " + file.getAbsolutePath() +
                                       " created");
                    marshallIntoFile(renters, file);
                }
            } else {
                System.out.println("File " + file.getAbsolutePath() +
                                   " is being updated");
                appendMarshall(renters, file);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            e.printStackTrace();
        } catch (IOException ioe) {
            System.out.println("Some ioe exception!");
            ioe.printStackTrace();
        }

    }//serialize()

    //Overwrites outFile content with renters collection

    private static void marshallIntoFile(Renters renters, File outFile) {
        try {
            JAXBContext context =
                    JAXBContext.newInstance(renters.getClass());
            Marshaller m =
                    context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
            m.marshal(renters, new FileOutputStream(outFile));
            System.out.println("From Marshall!");
        } catch (JAXBException e) {
            System.out.println("JAXB problem!");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //Appends renters to outFile content

    private static void appendMarshall(Renters renters,
                                       File outFile) {
        Renters temp = new Renters();
        List<Renter> oldRenters = Deserializer.deserialize(outFile.getAbsolutePath()).getRenter();
        List<Renter> newRenters = renters.getRenter();
        temp.getRenter().addAll(oldRenters);
        temp.getRenter().addAll(newRenters);

        marshallIntoFile(temp, outFile);
        System.out.println("From AppendMarshall!");
    }

    //===============================================================================================

    public static void serialize(IXmlCollectable collection, String outFilePath) {
        File file = new File(outFilePath);

        if (!file.exists()) {
            try {
                if (file.createNewFile()) {
                    System.out.println("Created new file " + file.getAbsolutePath());
                    marshallIntoFile(collection, file);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            //append .xml content to existing file
        }

    }

    private static void marshallIntoFile(IXmlCollectable collection, File file) {
        try {
            JAXBContext context =
                    JAXBContext.newInstance(collection.getClass());
            Marshaller m =
                    context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
            m.marshal(collection, new FileOutputStream(file));
            System.out.println("From Marshall!");
        } catch (JAXBException e) {
            System.out.println("JAXB problem!");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    //===============================================================================================
}//class Serializer
